﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Tracy.CoreLib.Data
{
    public class SiteSchema : ManagedXmlModel_CommonFunctions, IManagedXmlModelItem
    {
        #region 构造函数
        public SiteSchema() : base() { }
        public SiteSchema(string xml) : base(xml) { }
        public SiteSchema(XmlNode node) : base(node) { }
        #endregion

        public string Title
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_TITLE)) return Data[Constants.CONST_TAG_TITLE];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_TITLE] = value;
            }
        }

        public string Description
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_DESCRIPTION)) return Data[Constants.CONST_TAG_DESCRIPTION];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_DESCRIPTION] = value;
            }
        }

        public string Target
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_TARGET)) return Data[Constants.CONST_TAG_TARGET];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_TARGET] = value;
            }
        }

        public string Type
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_TYPE)) return Data[Constants.CONST_TAG_TYPE];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_TYPE] = value;
            }
        }

        private Filter filter;

        public Filter Filter
        {
            get { if (filter == null)filter = new Filter(); return filter; }
            set { filter = value; }
        }


        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_ITEM; }
        }

        protected override void PreParseXml(XmlNode node)
        {
            Title = SelectNode(node, Constants.CONST_TAG_TITLE, true).InnerText;
            //description = SelectNode(node, Constants.CONST_TAG_DESCRIPTION, true).InnerText;
            Type = SelectNode(node, Constants.CONST_TAG_TYPE, true).InnerText;
            //XmlNode targetNode = SelectNode(node, Constants.CONST_TAG_TARGET);
            //if(targetNode != null) target = targetNode.InnerText;
            XmlNode filterNode = SelectNode(node, Constants.CONST_TAG_FILTER, true);
            filter = new Filter(filterNode);
        }

        protected override void PostParseXml(XmlNode node)
        {
            //兼容旧版Filter
            if (Data.ContainsKey(Constants.CONST_TAG_FILTER))
            {
                Data.Remove(Constants.CONST_TAG_FILTER);
                string key = Constants.CONST_TAG_LEFTPOS + "@" + Constants.CONST_TAG_FILTER;
                if (Data.ContainsKey(key))
                {
                    Data.Remove(key);
                }
                key = Constants.CONST_TAG_RIGHTPOS + "@" + Constants.CONST_TAG_FILTER;
                if (Data.ContainsKey(key))
                {
                    Data.Remove(key);
                }
            }
        }

        protected override void PostGenerateXml(XmlDocument doc, XmlNode node)
        {
            //AppendTextChildNode(doc, node, Constants.CONST_TAG_TITLE, Title);
            //AppendTextChildNode(doc, node, Constants.CONST_TAG_DESCRIPTION, Description);
            //AppendTextChildNode(doc, node, Constants.CONST_TAG_TYPE, Type);
            //if(Target != null) AppendTextChildNode(doc, node, Constants.CONST_TAG_TARGET, Target);
            node.AppendChild(filter.ToXmlNode(doc));
        }

        public override string ToString()
        {
            return Title;
        }

        #region IKeyedItem Members

        public string Key
        {
            get { return Id.ToString(); }
        }

        #endregion
    }

    public class SiteSchemaProvider : XmlModelProvider_CommonFunctions, IXmlModelProvider
    {
        #region 构造函数
        public SiteSchemaProvider() : base() { }
        public SiteSchemaProvider(string xml) : base(xml) { }
        public SiteSchemaProvider(XmlNode node) : base(node) { }
        #endregion

        public IXmlModelItem GenerateChild(XmlNode node)
        {
            return new SiteSchema(node);
        }

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_SITESCHEMALIST; }
        }

    }
}
